const Request = require("../../utils/util");//导入模块
Page({

  /**
   * 页面的初始数据
   */
  data: {
    source: ['自然到店', '小区地推', '电话营销', '朋友介绍', '装修公司', '广告推广', '邀约到店', '渠道'],
    customerLevel: ['中意向', '高意向', '准意向'],
    grade: ['低端', '中端', '高端'],
    decorateCategory: ['窗帘', '墙布', '挂画', '饰品', '定制', '软包', '硬包'],
    regionId: ['嘉陵区', '顺庆区', '高坪区', '西充', '营山', '成都'],
    type: ['精装房', '清水房', '完工清水房', '老房翻新'],
    meetType: ['到店见面', '上门拜访'],

    date: Request.dateTime(),
    countPic: 3,//上传图片最大数量
    showImgUrl: Request.imgHttp, //路径拼接，一般上传返回的都是文件名，
    uploadImgUrl: Request.apiHttp + 'fileUpload/uploadfile',//图片的上传的路径
    picsList: [], //图片上传成功的name
    parameter: {
      name: '', //客户姓名
      phone: '', //手机号码
      secondPhone: '',//备用手机号码
      build: '',//楼盘
      unit: '',//单元栋号
      source: 0,//1.自然到店 2.小区地推3.电话营销4.朋友介绍5.装修公司6.广告推广7.邀约到店8.渠道
      weChat: '',//微信
      referees: '',// 推荐人
      customerLevel: 0, //客户级别
      grade: 0,//客户档次 1.低端，2.中端，3.高端
      decorateCategory: 0,//1.窗帘，2.墙布，3.挂画，4.饰品，5.定制，6.软包，7.硬包
      regionId: 0,//区域位置
      area: '',//房屋面积
      type: 0,//1.精装房，2.清水房，3.完工清水房，4.老房翻新
      demand: '',//客户需求
      remark: '', //备注
      isMeet: true,//是否见面
      meetAddress: '',// 见面信息中得备注
      meetTime: Request.dateTime(), // 见面时间
      meetType: 0, //见面类型
      firstConsultTime: Request.dateTime(), //首次咨询时间
      completionDate: Request.dateTime(),//交房时间
      detailAddress: '', // 详细地址
      decorateStyle: '',//装修风格
      buildStructure: '',//房屋结构
      visitStores: '',//到访门店
      houseType: '',//户型
    }
  },


  //监听组件事件，返回的结果
  myEventListener: function (e) {
    this.setData({ picsList: e.detail.picsList })
  },


  /**
   * 见面时间 
   */
  bindmeetTimeChange: function (e) {
    let m = "parameter.meetTime";
    this.setData({
      [m]: e.detail.value
    })
  },
  /**
   * 首次咨询时间
   */
  bindFirstConsultTimeChange: function (e) {
    let m = "parameter.firstConsultTime";
    this.setData({
      [m]: e.detail.value
    })
  },
  /**
   * 交房时间
   */
  bindCompletionDateChange: function (e) {
    let m = "parameter.completionDate";
    this.setData({
      [m]: e.detail.value
    })
  },


  //////////////////数据绑定//////////////////////
  nameValue: function (e) {
    this.data.parameter.name = e.detail.value;
  },

  phoneValue: function (e) {
    this.data.parameter.phone = e.detail.value;
  },
  secondPhoneValue: function (e) {
    this.data.parameter.secondPhone = e.detail.value;
  },
  buildValue: function (e) {
    this.data.parameter.build = e.detail.value;
  },
  unitValue: function (e) {
    this.data.parameter.unit = e.detail.value;
  },
  weChatValue: function (e) {
    this.data.parameter.weChat = e.detail.value;
  },
  refereesValue: function (e) {
    this.data.parameter.referees = e.detail.value;
  },
  areaValue: function (e) {
    this.data.parameter.area = e.detail.value;
  },
  demandValue: function (e) {
    this.data.parameter.demand = e.detail.value;
  },
  remarkValue: function (e) {
    this.data.parameter.remark = e.detail.value;
  },
  meetAddressValue: function (e) {
    this.data.parameter.meetAddress = e.detail.value;
  },
  detailAddressValue: function (e) {
    this.data.parameter.detailAddress = e.detail.value;
  },
  decorateStyleValue: function (e) {
    this.data.parameter.decorateStyle = e.detail.value;
  },
  buildStructureValue: function (e) {
    this.data.parameter.buildStructure = e.detail.value;
  },
  visitStoresValue: function (e) {
    this.data.parameter.visitStores = e.detail.value;
  },
  houseTypeValue: function (e) {
    this.data.parameter.houseType = e.detail.value;
  },


  bindSourceChange: function (e) {
    let s = "parameter.source";
    this.setData({
      [s]: e.detail.value
    })
  },

  bindCustomerLevelChange: function (e) {
    let c = "parameter.customerLevel";
    this.setData({
      [c]: e.detail.value
    })
  },
  bindGradeChange: function (e) {
    let g = "parameter.grade";
    this.setData({
      [g]: e.detail.value
    })
  },
  bindDecorateCategoryChange: function (e) {
    let d = "parameter.decorateCategory";
    this.setData({
      [d]: e.detail.value
    })
  },
  bindRegionIdChange: function (e) {
    let r = "parameter.regionId";
    this.setData({
      [r]: e.detail.value
    })
  },
  bindTypeChange: function (e) {
    let t = "parameter.type";
    this.setData({
      [t]: e.detail.value
    })
  },
  bindMeetTypeChange: function (e) {
    let t = "parameter.meetType";
    this.setData({
      [t]: e.detail.value
    })
  },


  switch1Change: function (e) {
    let t = "parameter.isMeet";
    this.setData({
      [t]: e.detail.value
    });
  },

  ///////////////////////事件///////////////////////
  /**
   * 提交
   */
  submit: function () {
    if (this.data.parameter.name.length < 1) {
      wx.showToast({
        title: "请输入客户姓名",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.parameter.phone.length < 1) {
      wx.showToast({
        title: "请输入客户手机号码",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.parameter.build.length < 1) {
      wx.showToast({
        title: "请输入客户楼盘",
        icon: 'none',
        duration: 1000
      })
      return
    }

    if (this.data.parameter.unit.length < 1) {
      wx.showToast({
        title: "请输入单元栋号",
        icon: 'none',
        duration: 1000
      })
      return
    }

    wx.showLoading({
      title: '保存中',
      mask: true,
    })

    let p = this.data.parameter;
    p.source = parseInt(p.source) + 1;
    p.customerLevel = parseInt(p.customerLevel) + 1;
    p.grade = parseInt(p.grade) + 1;
    p.decorateCategory = parseInt(p.decorateCategory) + 1;
    p.regionId = parseInt(p.regionId) + 1;
    p.type = parseInt(p.type) + 1;
    p.meetType = parseInt(p.meetType) + 1;

    p.attachment = JSON.stringify(this.data.picsList);
    p.emCustomerExt = {
      meetAttachment: "",
      meetType: p.meetType,
      isMeet: p.isMeet ? 1 : 2,
      meetTime: new Date(p.meetTime),
      meetAddress: p.meetAddress,
      customerLevel: p.customerLevel,
      firstConsultTime: new Date(p.firstConsultTime),
      detailAddress: p.detailAddress,
      buildStructure: p.buildStructure,
      visitStores: p.visitStores,
      houseType: p.houseType,
      completionDate: new Date(p.completionDate),
    }

    Request.post("emCustomer/insertCustomer", p).then(res => {
      wx.hideLoading();
      wx.showToast({
        title: '保存成功',
        icon: 'none',
        duration: 1000
      })
      wx.navigateBack({});

    }).catch(err => {
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })


  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})